# 初始化数据库的文件，给user_info插入若干条随机生成的信息
from hashlib import md5
import mysql.connector
import PW
import random
a_ascii = 97
z_ascii = 122


def getAPhoneNumber():  # 随机电话号码生成
    second = random.choice([3, 4, 5, 7, 8])
    third = {
        3: random.randint(0, 9),
        4: random.choice([5, 7, 9]),
        5: random.choice([i for i in range(10) if i != 4]),
        7: random.choice([i for i in range(9) if i != 4]),
        8: random.randint(0, 9),
    }[second]

    last = "".join(str(random.randint(0, 9)) for i in range(8))
    return "1{}{}{}".format(second, third, last)


val = []
for i in range(1, 100):
    temp_id = "%08d" % (i)
    temp_phone = getAPhoneNumber()
    temp_name = ""
    temp_password = ""
    for j in range(6):
        temp_name = temp_name + chr(random.randint(a_ascii, z_ascii))
    # 以用户名称作为密码，计算其md5插入到数据库中
    temp_password = md5(temp_name.encode('utf-8')).hexdigest()
    val.append((temp_id, temp_phone, temp_name, temp_password))
conn = mysql.connector.connect(
    user=PW.my_username, password=PW.my_password, database=PW.database)  # 数据库连接
cursor = conn.cursor()
sql = "INSERT INTO user_info VALUES (%s,%s,%s,%s)"
cursor.executemany(sql, val)
conn.commit()
print(cursor.rowcount, 'success')
# input("continue")
# cursor.execute("TRUNCATE TABLE user_info")  # 清空表格
# print(cursor.rowcount, 'success')

conn.close()
